// 程序变量
var httpUrl;
var captchaToken;
var grouponId;
var addrId;
var phoneNum;

//换购主方法
function huangou(){
	httpUrl = 'http://t.yhd.com/tuangou/ajax/ajaxSmsSendForHuangou.do';
	$.ajax({
		url:httpUrl,
		type:'get',
		dataType:'json',
		success:function(data){
			if(data.code!=1){
				alert(data.msg);
			}else{//验证码发送成功
				phoneNum = data.data;
                timeOut= timeOut-1200;
				setTimeout(getRd,timeOut); //自动豪华
			}
		}
	});
}

/**
 * 取验证码，取成功了就换购
 * @return {[type]} [description]
 */
function getRd(){
	chrome.extension.sendMessage({greeting:"smsCode",No:phoneNum},function(response){
		console.log('获取到了验证码：'+ response.smsCode);
  		if(response.smsCode!='error'){
  			goHuan(response.smsCode);
  		}
	});
}
/**
 * 换购提交
 * @param  {[type]} rd [description]
 * @return {[type]}    [description]
 */
function goHuan(rd){
	$.ajax({
        type: "POST",
        url:  "http://t.yhd.com/tuangou/seckill.do",
        dataType:'json',
        traditional: true,
        data: {
        	orderInfoId:addrId,
        	verifyCode:null,
        	serialId:0,
        	verifySig:0,
			captchaToken:captchaToken,
			buyNums:1,
			invokeType:0,
			grouponId:grouponId,
			verifyCodeForHuangou:rd,
			orderCodesForHuangou:orderCodesForHuangou.split(",")
        },
        success: function(responseData){
        	if(responseData.code==1){ //普通秒杀下单成功
        		var orderCode = responseData.data;
        		var url = "http://my.yhd.com/order/finishOrder.do?orderCode="+orderCode;
        		window.location=url;
        	}else if(responseData.code==50){//排队秒杀排队成功
        		var token = responseData.data;
        		var url = "http://cart.yhd.com/cart/yqs/waiting.do?token="+token+"&returnUrl=";
        		window.location=url;
        	}else if(responseData.code==51){//排队秒杀排队不成功
        		//alert(responseData.message);
        		var url = "http://cart.yhd.com/cart/yqs/waiting.do?status=fail&code="+responseData.code;
        		window.location=url;
        	}else if(responseData.code==60){//排队秒杀,排队人数达到上限
        		//alert(responseData.message);
        		//跳到购物车
        		var url = "http://cart.yhd.com/cart/yqs/waiting.do?status=fail&code="+responseData.code;
        		window.location=url;
        	}else if(responseData.code==61){//排队秒杀,用户购买数达到上限
        		//alert(responseData.message);
        		//跳到购物车
        		var url = "http://cart.yhd.com/cart/yqs/waiting.do?status=fail&code="+responseData.code;
        		window.location=url;
        	}else{
        		console.log(responseData.message);
        	}
        }
    });
}

//Main方法
(function(){
    var curUrl = window.location.href;
    if(curUrl.indexOf("seckill") == -1){
        console.log("当前页不是换购页！",new Date().getTime());
        timeOut = timeOut;
        setTimeout(function(){
            console.log(new Date().getTime());
        },timeOut);
        return;
    }
    var reg = /(\d+)/g;
    grouponId = curUrl.match(reg)[0];
    addrId = $('#seckillList').find('.flash_info').attr('data-receiverid');
    captchaToken = $('#__yct_str__').val();
    console.log('延迟请求换购验证码');
    setTimeout(huangou,1200);
})();